﻿Imports lbGlobalcom
Public Class fNDDevolMcia
    Dim daMovimi As lbGlobalcom.dsMovimientoTableAdapters.MOV_MOVIMIENTOTableAdapter
    Dim daMovimiDetalle As lbGlobalcom.dsMovimientoTableAdapters.MOV_MOVIMIENTODETALLETableAdapter
    Dim daMivimiDetDescri As lbGlobalcom.dsMovimientoTableAdapters.MOV_DESCRIPCIONDETTableAdapter
    Dim daTipoDoc As lbGlobalcom.dsGlobalTableAdapters.GLO_TIPODOCUMENTOTableAdapter
    Dim daTerce As lbGlobalcom.dsTerceroTableAdapters.TER_TERCEROTableAdapter
    Dim daProv As lbGlobalcom.dsTerceroTableAdapters.TER_PROVEEDORESTableAdapter
    Dim daCli As lbGlobalcom.dsTerceroTableAdapters.TER_CLIENTESTableAdapter
    Dim daVen As lbGlobalcom.dsTerceroTableAdapters.TER_VENDEDORTableAdapter
    Dim daTipoSubProd As lbGlobalcom.dsGlobalTableAdapters.GLO_DATOSTABLASTableAdapter
    Dim drProv As lbGlobalcom.dsTercero.TER_PROVEEDORESRow
    Dim drCli As lbGlobalcom.dsTercero.TER_CLIENTESRow
    Dim drVen As lbGlobalcom.dsTercero.TER_VENDEDORRow
    Dim cListaP As lbGlobalcom.cEncaListaPrecios
    Dim cDetLis As lbGlobalcom.cDetalleListaPrecios
    'Dim cMov As New lbGlobalcom.cMovimiento
    Dim cCom As lbGlobalcom.cCompra
    Dim iTipoDoc As cTipoDocumento
    Dim iPer As cPeriodos
    Dim iBodOri As cBodegas
    Dim iBodDes As cBodegas
    Dim iFP As cFormasPago
    Dim iTerce As cTercero
    Dim iCliente As cClientes
    Dim lEliminaDoc As Boolean

    Private Sub fNDDevolMcia_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Try
            If DsMovimiento.MOV_DESCRIPCIONDET.GetChanges IsNot Nothing Or DsMovimiento.MOV_MOVIMIENTODETALLE.GetChanges IsNot Nothing Or DsMovimiento.MOV_MOVIMIENTO.GetChanges IsNot Nothing Then
                If MessageBox.Show("Hay cambios pendientes por salvar, desae salvarlos", "Cambios Pendientes", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
                    SalvaCambios(False)
                End If
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub


    Private Sub fMovimiento_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            daMovimi = New lbGlobalcom.dsMovimientoTableAdapters.MOV_MOVIMIENTOTableAdapter
            daMovimiDetalle = New lbGlobalcom.dsMovimientoTableAdapters.MOV_MOVIMIENTODETALLETableAdapter
            daMivimiDetDescri = New lbGlobalcom.dsMovimientoTableAdapters.MOV_DESCRIPCIONDETTableAdapter
            daTipoDoc = New lbGlobalcom.dsGlobalTableAdapters.GLO_TIPODOCUMENTOTableAdapter
            daTerce = New lbGlobalcom.dsTerceroTableAdapters.TER_TERCEROTableAdapter
            daTipoSubProd = New lbGlobalcom.dsGlobalTableAdapters.GLO_DATOSTABLASTableAdapter
            lbGlobalcom.cGeneral.ActivaControles(Me.Controls, False)
            DsMovimiento.MOV_MOVIMIENTO.Columns("FECHADOCUMENTO").DefaultValue = Date.Today
            DsMovimiento.MOV_MOVIMIENTO.Columns("FECHAVENCIMIENTO").DefaultValue = Date.Today
            iPer = New cPeriodos()
            dtDesdeF.Value = Date.Today.AddMonths(-6)
            daTipoSubProd.FillByIdGrupo(DsGlobal.GLO_DATOSTABLAS, "sp")
            cGeneral.BloquearOrdenDatagrid(MOV_DESCRIPCIONDETALLEDataGridView)
            cGeneral.BloquearOrdenDatagrid(MOV_MOVIMIENTODETALLEDataGridView)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub btnBuscaBodOri_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscaBodOri.Click
        Try
            Dim fBusBod As New fListaBodegas
            fBusBod.idSede = IDSEDETextBox.Text
            If fBusBod.ShowDialog = Windows.Forms.DialogResult.OK Then
                IDBODEGAORIGENextBox.Text = fBusBod.Bodega
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub btnBuscaCanal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscaCanal.Click
        Try
            Dim fLC As New fListaCanales
            If fLC.ShowDialog = Windows.Forms.DialogResult.OK Then
                IDCANALTextBox.Text = fLC.Canal
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub btnBuscaCCosto_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscaCCosto.Click
        Try
            Dim fCC As New fListaCCostos
            If fCC.ShowDialog = Windows.Forms.DialogResult.OK Then
                IDCCOSTOTextBox.Text = fCC.CCosto
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub btnBuscaLista_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscaLista.Click
        Try
            Dim fLP As New fListaPrecios
            If fLP.ShowDialog = Windows.Forms.DialogResult.OK Then
                IDLISTATextBox.Text = fLP.Lista
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub
    Private Sub CargaTipoDoc(ByVal TipoDoc As String)
        Try
            iTipoDoc = New cTipoDocumento(TipoDoc)
            If iTipoDoc Is Nothing Then
                MessageBox.Show("El tipo de documento seleccionado no esta registrado", "Error en tipo de documento", MessageBoxButtons.OK, MessageBoxIcon.Stop)
            Else
                NUMEROTextBox.Text = lbGlobalcom.Consec.GetNumeroActual(TipoDoc, iTipoDoc.DocEs)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub
    Private Sub ConfiguraByTipoDoc()
        Select Case iTipoDoc.DocEs
            Case 1
                btnBuscaBodOri.Enabled = False
                IDBODEGAORIGENextBox.Enabled = False
                IDTERCEROTextBox.Enabled = False
                SUCURSALTextBox.Enabled = False
                IDCANALTextBox.Enabled = False
                btnBuscaCanal.Enabled = False
            Case 2
                btnBuscaBodOri.Enabled = True
                IDBODEGAORIGENextBox.Enabled = True
                IDTERCEROTextBox.Enabled = False
                SUCURSALTextBox.Enabled = False
                IDCANALTextBox.Enabled = False
                btnBuscaCanal.Enabled = False
            Case 3
                btnBuscaBodOri.Enabled = True
                IDBODEGAORIGENextBox.Enabled = True
                IDTERCEROTextBox.Enabled = False
                SUCURSALTextBox.Enabled = False
                IDCANALTextBox.Enabled = False
                btnBuscaCanal.Enabled = False
            Case 4
                btnBuscaBodOri.Enabled = True
                IDBODEGAORIGENextBox.Enabled = True
                IDTERCEROTextBox.Enabled = True
                SUCURSALTextBox.Enabled = True
                IDCANALTextBox.Enabled = True
                btnBuscaCanal.Enabled = True
            Case 5
                btnBuscaBodOri.Enabled = False
                IDBODEGAORIGENextBox.Enabled = False
                IDTERCEROTextBox.Enabled = True
                SUCURSALTextBox.Enabled = False
                IDCANALTextBox.Enabled = False
                btnBuscaCanal.Enabled = False
        End Select
    End Sub
    Private Sub ActualizaDatosDoc()
        Try
            IDBODEGAORIGENextBox.Text = iTipoDoc.BodegaDe
            IDCCOSTOTextBox.Text = iTipoDoc.idCcosto
            IDLISTATextBox.Text = iTipoDoc.idLista
            IDSEDETextBox.Text = iTipoDoc.idSede
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
    Private Sub btnBuscaDoc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscaDoc.Click
        Try
            Dim fLDoc As New fListaTipoDocs
            fLDoc.SubClase = "NDC"
            If fLDoc.ShowDialog = Windows.Forms.DialogResult.OK Then
                IDDOCUMENTOTextBox.Text = fLDoc.idTipoDoc
                CargaTipoDoc(fLDoc.idTipoDoc)
                ConfiguraByTipoDoc()
                ActualizaDatosDoc()
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub ActualizaDatosTercero()
        Try
            iTerce = New cTercero(IDTERCEROTextBox.Text, "Pro")
            If iTerce IsNot Nothing Then
                txtNombreTercero.Text = iTerce.NombreCompleto
                TXTDIRECCION.Text = iTerce.Direccion
                Dim daMuni As New lbGlobalcom.dsGlobalTableAdapters.GLO_MUNICIPIOSTableAdapter
                TXTCIUDAD.Text = iTerce.iMunicipios.Municipio
                If iTipoDoc.DocEs = eTipoMovimi.NDCompras Then
                    If iTerce.iProveedor Is Nothing Then
                        MessageBox.Show("Proveedor no existe verifique e intente de nuevo", "Cliente No Existe", MessageBoxButtons.OK, MessageBoxIcon.Stop)
                    End If
                End If
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub
    Private Sub btnBuscaTerce_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscaTerce.Click
        Try
            Dim fBT As New fBuscaTercero
            fBT.TipoTercero = "Proveedor"
            If fBT.ShowDialog = Windows.Forms.DialogResult.OK Then
                IDTERCEROTextBox.Text = fBT.midTercero
                SUCURSALTextBox.Text = fBT.mSucursal
                iTerce = New cTercero(IDTERCEROTextBox.Text, "Pro")
                ActualizaDatosTercero()
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub btnBuscasede_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscasede.Click
        Try
            Dim fLS As New fListaSedes
            If fLS.ShowDialog = Windows.Forms.DialogResult.OK Then
                IDSEDETextBox.Text = fLS.Sede
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub


    Private Sub BindingNavigatorAddNewItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingNavigatorAddNewItem.Click
        Try
            lbGlobalcom.cGeneral.ActivaControles(Me.Controls, True)
            If DsMovimiento.MOV_MOVIMIENTO.GetChanges IsNot Nothing Or DsMovimiento.MOV_MOVIMIENTODETALLE.GetChanges IsNot Nothing Or DsMovimiento.MOV_DESCRIPCIONDET.GetChanges IsNot Nothing Then
                If MessageBox.Show("Hay cambios pendientes por salvar, desea salvarlos", "Cambios pendientes", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
                    SalvaCambios(True)
                End If
            End If
            MOV_MOVIMIENTOBindingSource.CancelEdit()
            DsMovimiento.MOV_DESCRIPCIONDET.Rows.Clear()
            DsMovimiento.MOV_MOVIMIENTODETALLE.Rows.Clear()
            DsMovimiento.MOV_MOVIMIENTO.Rows.Clear()
            MOV_MOVIMIENTOBindingSource.AddNew()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub MOV_MOVIMIENTODETALLEDataGridView_CellBeginEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellCancelEventArgs) Handles MOV_MOVIMIENTODETALLEDataGridView.CellBeginEdit
        MOV_MOVIMIENTODETALLEDataGridView.Rows(e.RowIndex).Cells("LINEA").Value = cGeneral.GetNumeroLinea(MOV_MOVIMIENTODETALLEDataGridView, e.RowIndex)
    End Sub
    Private Sub ActualizaDatosProd(ByVal Fila As Integer)
        Try
            If MOV_MOVIMIENTODETALLEDataGridView.CurrentRow IsNot Nothing Then
                MOV_MOVIMIENTODETALLEDataGridView.Rows(Fila).Cells(8).Value = cDetLis.PrecioCosto
                MOV_MOVIMIENTODETALLEDataGridView.Rows(Fila).Cells(9).Value = cDetLis.PrecioCosto * cDetLis.iProducto.iTipoIva.PorIva
                Dim idTerce As String = ""
                If iBodOri.ExistenciaCliente.ToUpper = "S" Then
                    idTerce = IDTERCEROTextBox.Text
                Else
                    idTerce = "0"
                End If
                Dim iInv As cInventarios = New cInventarios(DsMovimiento, iPer.IDPERIODO, IDBODEGAORIGENextBox.Text, MOV_MOVIMIENTODETALLEDataGridView.Rows(Fila).Cells("IDPRODUCTO").Value, MOV_MOVIMIENTODETALLEDataGridView.Rows(Fila).Cells("LINEA").Value, idTerce, 0)
                cDetLis = cListaP.DetalleLista(MOV_MOVIMIENTODETALLEDataGridView.Rows(Fila).Cells("IDPRODUCTO").Value, IDCANALTextBox.Text)
                stMovimi.Text = "PRODUCTO: " & cDetLis.iProducto.Descripcion & " UNIDAD: " & cDetLis.iProducto.Unidad & " CONTENIDO: " & cDetLis.iProducto.Contenido & " EXISTENCIAS: " & iInv.Existencia
            Else
                stMovimi.Text = ""
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub MOV_MOVIMIENTODETALLEDataGridView_CellMouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles MOV_MOVIMIENTODETALLEDataGridView.CellMouseDoubleClick
        Try
            Select Case e.ColumnIndex
                Case 3
                    If MOV_MOVIMIENTODETALLEDataGridView.CurrentCellAddress.X = 3 Then
                        Dim fLProd As New fListaProductos
                        fLProd.ListaPrecios = IDLISTATextBox.Text
                        If fLProd.ShowDialog = Windows.Forms.DialogResult.OK Then
                            MOV_MOVIMIENTODETALLEDataGridView.BeginEdit(True)
                            MOV_MOVIMIENTODETALLEDataGridView.Rows(MOV_MOVIMIENTODETALLEDataGridView.CurrentRow.Index).Cells(3).Value = fLProd.idArticulo
                            MOV_MOVIMIENTODETALLEBindingSource.EndEdit()
                            cDetLis = cListaP.DetalleLista(fLProd.idArticulo, IDCANALTextBox.Text)
                            ActualizaDatosProd(MOV_MOVIMIENTODETALLEDataGridView.CurrentCellAddress.Y)
                            stMovimi.Text = "PRODUCTO: " & cDetLis.iProducto.Descripcion & " UNIDAD: " & cDetLis.iProducto.Unidad & " CONTENIDO: " & cDetLis.iProducto.Contenido
                            'MOV_MOVIMIENTODETALLEDataGridView.BeginEdit(True)
                        End If
                    End If
            End Select
        Catch ex As Exception

        End Try
    End Sub

    Private Sub MOV_MOVIMIENTODETALLEDataGridView_CellValidated(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles MOV_MOVIMIENTODETALLEDataGridView.CellValidated
        If MOV_MOVIMIENTODETALLEDataGridView.IsCurrentCellInEditMode Then
            Select Case e.ColumnIndex
                Case 3
                    If iTipoDoc.DocEs <> eTipoMovimi.Venta Then
                        ActualizaDatosProd(e.RowIndex)
                    End If
                    Dim cDescDtalle As cCargarDescripcionDtalle = New cCargarDescripcionDtalle(DsMovimiento, IDBODEGAORIGENextBox.Text, IDDOCUMENTOTextBox.Text, MOV_MOVIMIENTODETALLEDataGridView.Rows(e.RowIndex).Cells(2).Value, NUMEROTextBox.Text, MOV_MOVIMIENTODETALLEBindingSource.Position)
                    cDescDtalle.cargaDescripcionDtalle()
                    MOV_DESCRIPCIONDETALLEBindingSource.EndEdit()
                    SalvaCambios(False)
                Case 5
                    MOV_MOVIMIENTODETALLEBindingSource.EndEdit()
                    Dim cDescDtalle As cCargarDescripcionDtalle = New cCargarDescripcionDtalle(DsMovimiento, IDBODEGAORIGENextBox.Text, IDDOCUMENTOTextBox.Text, MOV_MOVIMIENTODETALLEDataGridView.Rows(e.RowIndex).Cells(2).Value, NUMEROTextBox.Text, MOV_MOVIMIENTODETALLEBindingSource.Position)
                    cDescDtalle.cargaDescripcionDtalle()
                    MOV_DESCRIPCIONDETALLEBindingSource.EndEdit()
                    SalvaCambios(False)
            End Select
        End If
    End Sub

    Private Sub MOV_MOVIMIENTODETALLEDataGridView_CellValidating(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles MOV_MOVIMIENTODETALLEDataGridView.CellValidating
        Try
            If MOV_MOVIMIENTODETALLEDataGridView.IsCurrentCellInEditMode AndAlso e.FormattedValue IsNot Nothing AndAlso e.FormattedValue <> "" Then
                Select Case e.ColumnIndex
                    Case 3
                        Dim idTerce As String = ""
                        If iBodOri.ExistenciaCliente.ToUpper = "S" Then
                            idTerce = IDTERCEROTextBox.Text
                        Else
                            idTerce = "0"
                        End If
                        Dim iInv As cInventarios = New cInventarios(DsMovimiento, iPer.IDPERIODO, IDBODEGAORIGENextBox.Text, MOV_MOVIMIENTODETALLEDataGridView.CurrentRow.Cells("IDPRODUCTO").Value, MOV_MOVIMIENTODETALLEDataGridView.CurrentRow.Cells("LINEA").Value, idTerce, 0)
                        cDetLis = cListaP.DetalleLista(e.FormattedValue, IDCANALTextBox.Text)
                        stMovimi.Text = "PRODUCTO: " & cDetLis.iProducto.Descripcion & " UNIDAD: " & cDetLis.iProducto.Unidad & " CONTENIDO: " & cDetLis.iProducto.Contenido & " EXISTENCIAS: " & iInv.Existencia
                    Case 5
                        'revisar cambio
                        'If iTipoDoc.DocEs = eTipoMovimi.Entrada Or iTipoDoc.DocEs = eTipoMovimi.Salida Or iTipoDoc.DocEs = eTipoMovimi.Traslado Or iTipoDoc.DocEs = eTipoMovimi.Venta Then
                        If iTipoDoc.DocEs = eTipoMovimi.Salida Or iTipoDoc.DocEs = eTipoMovimi.Traslado Or iTipoDoc.DocEs = eTipoMovimi.Venta Then
                            Dim idTerce As String = ""
                            If iBodOri.ExistenciaCliente.ToUpper = "S" Then
                                idTerce = IDTERCEROTextBox.Text
                            Else
                                idTerce = "0"
                            End If
                            Dim iInv As cInventarios = New cInventarios(DsMovimiento, iPer.IDPERIODO, IDBODEGAORIGENextBox.Text, MOV_MOVIMIENTODETALLEDataGridView.CurrentRow.Cells("IDPRODUCTO").Value, MOV_MOVIMIENTODETALLEDataGridView.CurrentRow.Cells("LINEA").Value, idTerce, 0)
                            Dim drMov As dsMovimiento.MOV_MOVIMIENTODETALLERow
                            If DsMovimiento.MOV_MOVIMIENTODETALLE.Rows.Count - 1 < MOV_MOVIMIENTODETALLEBindingSource.Position Then
                                drMov = Nothing
                            Else
                                drMov = DsMovimiento.MOV_MOVIMIENTODETALLE.Rows(MOV_MOVIMIENTODETALLEBindingSource.Position)
                            End If
                            If Not iInv.HayExistencias(drMov, CInt(e.FormattedValue)) Then
                                MessageBox.Show("Existencias insuficientes", "Existencias", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                                If iTipoDoc.existenciasNegativas.ToUpper = "N" Then
                                    e.Cancel = True
                                End If
                            Else
                                'MOV_MOVIMIENTODETALLEBindingSource.EndEdit()
                                'MOV_DESCRIPCIONDETALLEDataGridView.Rows.Clear()

                            End If
                        End If
                End Select
            End If
        Catch ex As Exception
            stMovimi.Text = ""
            e.Cancel = True
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub MOV_MOVIMIENTODETALLEDataGridView_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles MOV_MOVIMIENTODETALLEDataGridView.GotFocus
        Try
            MOV_MOVIMIENTOBindingSource.EndEdit()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub IDLISTATextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles IDLISTATextBox.TextChanged
        Try
            If IDLISTATextBox.Text.Trim.Length > 0 Then _
                cListaP = New cEncaListaPrecios(IDLISTATextBox.Text)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub MOV_MOVIMIENTODETALLEDataGridView_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MOV_MOVIMIENTODETALLEDataGridView.KeyDown
        Try
            If e.Control Then
                Select Case e.KeyCode
                    Case Keys.B
                        If MOV_MOVIMIENTODETALLEDataGridView.CurrentCellAddress.X = 3 Then
                            Dim fLProd As New fListaProductos
                            fLProd.ListaPrecios = IDLISTATextBox.Text
                            If fLProd.ShowDialog = Windows.Forms.DialogResult.OK Then
                                MOV_MOVIMIENTODETALLEDataGridView.BeginEdit(True)
                                MOV_MOVIMIENTODETALLEDataGridView.Rows(MOV_MOVIMIENTODETALLEDataGridView.CurrentRow.Index).Cells(3).Value = fLProd.idArticulo
                                MOV_MOVIMIENTODETALLEBindingSource.EndEdit()
                                cDetLis = cListaP.DetalleLista(fLProd.idArticulo, IDCANALTextBox.Text)
                                ActualizaDatosProd(MOV_MOVIMIENTODETALLEDataGridView.CurrentCellAddress.Y)
                                stMovimi.Text = "PRODUCTO: " & cDetLis.iProducto.Descripcion & " UNIDAD: " & cDetLis.iProducto.Unidad & " CONTENIDO: " & cDetLis.iProducto.Contenido
                                'MOV_MOVIMIENTODETALLEDataGridView.BeginEdit(True)
                            End If
                        End If
                End Select
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try

    End Sub



    Private Sub MOV_MOVIMIENTODETALLEDataGridView_RowEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles MOV_MOVIMIENTODETALLEDataGridView.RowEnter
        Try
            If e.RowIndex >= 0 AndAlso MOV_MOVIMIENTODETALLEDataGridView.Rows(e.RowIndex).Cells(3).Value IsNot Nothing AndAlso MOV_MOVIMIENTODETALLEDataGridView.Rows(e.RowIndex).Cells(1).Value IsNot DBNull.Value Then
                cDetLis = cListaP.DetalleLista(MOV_MOVIMIENTODETALLEDataGridView.Rows(e.RowIndex).Cells(3).Value, IDCANALTextBox.Text)
                stMovimi.Text = "PRODUCTO: " & cDetLis.iProducto.Descripcion & " UNIDAD: " & cDetLis.iProducto.Unidad & " CONTENIDO: " & cDetLis.iProducto.Contenido
                'ActualizaDatosProd(e.RowIndex)
                TotalizaDoc()
            Else
                stMovimi.Text = ""
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try

    End Sub
    Private Sub SalvaCambios(ByVal Msj As Boolean)
        Dim Conn As Oracle.DataAccess.Client.OracleConnection
        Dim tran As Oracle.DataAccess.Client.OracleTransaction
        Try
            Conn = New Oracle.DataAccess.Client.OracleConnection(My.Settings.ConnectionString)
            Conn.Open()
            tran = Conn.BeginTransaction
            If Msj Then _
                Validate()
            MOV_MOVIMIENTOBindingSource.EndEdit()
            MOV_MOVIMIENTODETALLEBindingSource.EndEdit()
            MOV_DESCRIPCIONDETALLEBindingSource.EndEdit()
            Dim AfecInv As New cAfectaIventarios(DsMovimiento, iTipoDoc, iPer, False, iTerce.iClientes, "N", Conn)
            AfecInv.AfectaInventarios()
            AfecInv = Nothing
            Dim SalvaMov As cSalvaMovimiento
            If lEliminaDoc Then
                SalvaMov = New cSalvaMovimiento(Conn, DsMovimiento.MOV_MOVIMIENTO.GetChanges, DsMovimiento.MOV_MOVIMIENTODETALLE.GetChanges, DsMovimiento.MOV_DESCRIPCIONDET.GetChanges, 0)
            Else
                SalvaMov = New cSalvaMovimiento(Conn, DsMovimiento.MOV_MOVIMIENTO.GetChanges, DsMovimiento.MOV_MOVIMIENTODETALLE.GetChanges, DsMovimiento.MOV_DESCRIPCIONDET.GetChanges, 1)
            End If
            lEliminaDoc = False
            DsMovimiento.AcceptChanges()
            SalvaMov = Nothing
            tran.Commit()
            Conn.Close()
            If iTipoDoc.TipoDocGenera IsNot DBNull.Value AndAlso iTipoDoc.TipoDocGenera.Trim.Length > 0 Then
                Dim DocGen As cGeneraDocumentoAutomatico = New cGeneraDocumentoAutomatico(iTipoDoc.TipoDocGenera, DsMovimiento)
                DocGen = Nothing
            End If
            If Msj Then _
                MessageBox.Show("Se salvarón cambios con exito", "Salvar Cambios", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Catch ex As Exception
            tran.Rollback()
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub
    Private Sub MOV_MOVIMIENTOBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MOV_MOVIMIENTOBindingNavigatorSaveItem.Click
        Try
            SalvaCambios(True)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub MOV_DESCRIPCIONDETALLEDataGridView_CellBeginEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellCancelEventArgs) Handles MOV_DESCRIPCIONDETALLEDataGridView.CellBeginEdit
        asignarLinea(e.RowIndex)
        If e.RowIndex + 1 > MOV_MOVIMIENTODETALLEDataGridView.CurrentRow.Cells(5).Value Then
            e.Cancel = True
            MessageBox.Show("Supero el tope de registro de IMEI e ICCID para esta linea", "Supero el Tope", MessageBoxButtons.OK, MessageBoxIcon.Information)
            MOV_DESCRIPCIONDETALLEDataGridView.CancelEdit()
        End If
    End Sub

    Private Sub asignarLinea(ByVal Fila As Integer)
        If Fila = 0 Then
            MOV_DESCRIPCIONDETALLEDataGridView.Rows(Fila).Cells("LINEADESCDET").Value = 1
        Else
            MOV_DESCRIPCIONDETALLEDataGridView.Rows(Fila).Cells("LINEADESCDET").Value = MOV_DESCRIPCIONDETALLEDataGridView.Rows(Fila - 1).Cells("LINEADESCDET").Value + 1
        End If
    End Sub

    Private Sub btnBuscaFactura_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscaFactura.Click
        Try
            Dim fListaMov As New fListaMovimi
            fListaMov.SubClase = "NCC"
            If fListaMov.ShowDialog = Windows.Forms.DialogResult.OK Then
                If DsMovimiento.MOV_DESCRIPCIONDET.GetChanges IsNot Nothing Or DsMovimiento.MOV_MOVIMIENTODETALLE.GetChanges IsNot Nothing Or DsMovimiento.MOV_MOVIMIENTO.GetChanges IsNot Nothing Then
                    If MessageBox.Show("Hay cambios pendientes por salvar, desae salvarlos", "Cambios Pendientes", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
                        SalvaCambios(False)
                    End If
                End If
                DsMovimiento.MOV_DESCRIPCIONDET.Rows.Clear()
                DsMovimiento.MOV_MOVIMIENTODETALLE.Rows.Clear()
                DsMovimiento.MOV_MOVIMIENTO.Rows.Clear()
                daMovimi.FillByIdDocumentoNumero(DsMovimiento.MOV_MOVIMIENTO, fListaMov.mTipoDoc, fListaMov.mNumeroDoc)
                daMovimiDetalle.FillByIdDocumentoNumero(DsMovimiento.MOV_MOVIMIENTODETALLE, fListaMov.mTipoDoc, fListaMov.mNumeroDoc)
                daMivimiDetDescri.FillByIdDocumentoNumero(DsMovimiento.MOV_DESCRIPCIONDET, fListaMov.mTipoDoc, fListaMov.mNumeroDoc)
                lbGlobalcom.cGeneral.ActivaControles(Me.Controls, True)
                TotalizaDoc()
                ActualizaDatosTercero()
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub BindingNavigatorDeleteItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingNavigatorDeleteItem.Click
        Try
            If MessageBox.Show("Esta seguro que desea eliminar este documento", "Elimina Documento", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
                MOV_MOVIMIENTOBindingSource.RemoveCurrent()
                lEliminaDoc = True
                SalvaCambios(False)
                MessageBox.Show("Se elimino documento con exito", "Elimino Documento", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub


    Private Sub IDBODEGAORIGENTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles IDBODEGAORIGENextBox.TextChanged
        Try
            If IDBODEGAORIGENextBox.Text.Trim.Length > 0 Then _
                iBodOri = New cBodegas(IDBODEGAORIGENextBox.Text)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub


    Private Sub IDDOCUMENTOTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles IDDOCUMENTOTextBox.TextChanged
        Try
            If IDDOCUMENTOTextBox.Text.Trim.Length > 0 Then _
                iTipoDoc = New cTipoDocumento(IDDOCUMENTOTextBox.Text)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try

    End Sub


    Private Sub TotalizaDoc()
        Dim TotalBase As Double = 0
        Dim TotalIva As Double = 0
        Dim Totaldoc As Double = 0
        For Each drDetMov As dsMovimiento.MOV_MOVIMIENTODETALLERow In DsMovimiento.MOV_MOVIMIENTODETALLE.Rows
            If Not drDetMov.RowState = DataRowState.Deleted Then
                TotalBase += drDetMov.VALORBASE * drDetMov.CANTIDAD
                TotalIva += drDetMov.VALORIVA * drDetMov.CANTIDAD
                Totaldoc += drDetMov.VALORLINEA
            End If

        Next
        Me.TXTTOTALBASE.Text = TotalBase.ToString("c2")
        Me.TXTTOTALIVA.Text = TotalIva.ToString("C2")
        Me.TXTTOTALDOC.Text = Totaldoc.ToString("C2")
    End Sub

    Private Sub MOV_DESCRIPCIONDETALLEDataGridView_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MOV_DESCRIPCIONDETALLEDataGridView.KeyDown
        Try
            If e.Control Then
                Select Case e.KeyCode
                    Case Keys.B
                        If MOV_DESCRIPCIONDETALLEDataGridView.CurrentCellAddress.X = 1 Then
                            Dim flMoviDtalle As New fListaMoviDetalle
                            flMoviDtalle.pBodegaDe = IDBODEGAORIGENextBox.Text
                            flMoviDtalle.pProducto = MOV_MOVIMIENTODETALLEDataGridView.Rows(MOV_MOVIMIENTODETALLEDataGridView.CurrentRow.Index).Cells(3).Value
                            If flMoviDtalle.ShowDialog = Windows.Forms.DialogResult.OK Then
                                'MessageBox.Show("selecciono")
                                MOV_DESCRIPCIONDETALLEDataGridView.Rows(MOV_DESCRIPCIONDETALLEDataGridView.CurrentRow.Index).Cells(2).Value = flMoviDtalle.pImei
                                MOV_DESCRIPCIONDETALLEDataGridView.Rows(MOV_DESCRIPCIONDETALLEDataGridView.CurrentRow.Index).Cells(3).Value = flMoviDtalle.pIccid
                                'MOV_MOVIMIENTODETALLEDataGridView.Rows(MOV_MOVIMIENTODETALLEDataGridView.CurrentRow.Index).Cells(3).Value = fLProd.idArticulo
                                asignarLinea(MOV_DESCRIPCIONDETALLEDataGridView.CurrentRow.Index)
                            End If
                        End If
                End Select
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub
    Private Sub MOV_DESCRIPCIONDETALLEDataGridView_DataError(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewDataErrorEventArgs) Handles MOV_DESCRIPCIONDETALLEDataGridView.DataError
        e.Cancel = True
        MessageBox.Show(e.Exception.Message)
    End Sub

    Private Sub IDTERCEROTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles IDTERCEROTextBox.TextChanged
        Try
            If IDTERCEROTextBox.Text.Trim.Length > 0 Then
                iTerce = New cTercero(IDTERCEROTextBox.Text, "Cli")
                If SUCURSALTextBox.Text.Trim.Length > 0 Then _
                iTerce.iClientes.SeekClienteBySucursal(SUCURSALTextBox.Text)
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    'Private Sub SUCURSALTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SUCURSALTextBox.TextChanged
    '    Try
    '        If SUCURSALTextBox.Text.Trim.Length > 0 AndAlso iTerce IsNot Nothing Then _
    '            iTerce.iClientes.SeekClienteBySucursal(SUCURSALTextBox.Text)
    '    Catch ex As Exception
    '        MessageBox.Show(ex.Message)
    '    End Try
    'End Sub

    Private Sub btnBusca_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBusca.Click
        If IDTERCEROTextBox.Text.Trim.Length > 0 Then
            MOV_MOVIMIENTOBindingSource.EndEdit()
            Dim ValoraNota As cValoraNotas = New cValoraNotas(IDTERCEROTextBox.Text, TXTIDPRODUCTO.Text, dtDesdeF.Value, IDDOCREFERENCIATextBox.Text, cGeneral.NZ(NUMEROREFERENCIATextBox.Text))
            'Dim dsConsMov As New dsConsultaMovimi
            Dim fBusca As New fListaArtbyNotaDev
            fBusca.DatosSelect = ValoraNota.InfoByNota
            If fBusca.ShowDialog = Windows.Forms.DialogResult.OK Then
                ValoraNota.InsertaProductosNota(DsMovimiento)
                ValoraNota = Nothing
            End If
        Else
            MessageBox.Show("Faltan datos para la busqueda, favor verificar e intentar de nuevo", "Faltan Datos", MessageBoxButtons.OK, MessageBoxIcon.Stop)
        End If

    End Sub

    
End Class